﻿using MES.Process.Domain;
using Microsoft.EntityFrameworkCore.Storage;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MES.Process.Infrastructure
{
    public class BaseRepository<T> : IBaseRepository<T> where T : BaseData
    {
        private readonly MESDBContext context;

        public BaseRepository(MESDBContext context)
        {
            this.context = context;
        }
        /// <summary>
        /// 开启EF事务
        /// </summary>
        public MESDBContext _context { get{ return context; } }

        public async Task<int> AddAsync(T entity)
        {
            await context.AddAsync(entity);
            return await context.SaveChangesAsync();
        }

        public async Task<int> AddRangeAsync(List<T> entity)
        {
            await context.AddRangeAsync(entity);
            return await context.SaveChangesAsync();
        }

        public async Task<int> DeleteAsync(T entity)
        {
             context.Remove(entity);
            return await context.SaveChangesAsync();
        }

        public async Task<int> DeleteRangeAsync(List<T> entity)
        {
            context.RemoveRange(entity);
            return await context.SaveChangesAsync();
        }

        public IQueryable<T> GetValues()
        {
            return context.Set<T>().AsQueryable();
        }

        public async Task<int> UpdateAsync(T entity)
        {
            context.Update(entity);
            return await context.SaveChangesAsync();
        }

        public async Task<int> UpdateRangeAsync(List<T> entity)
        {
            context.UpdateRange(entity);
            return await context.SaveChangesAsync();
        }
    }
}
